﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Serialization;
using ProgramMain.Database;

namespace ProgramMain.NetVis
{
     [XmlInclude(typeof(CableBoxLinkVis))]
    public class CableBoxVis : CrossBoxVis
    {
        [XmlIgnore]
        protected List<FibreRange> FibresSecond = new List<FibreRange>();

        [XmlIgnore]
        public int Index = 0;

       public override Size CalcSize()
        {
            return new Size(body.VirtualSize().Width + 10, body.LocationSize.Height + GetFibreHeight() * 2 + 10);
        }

        public override Point CalcBodyPoint()
        {
            return VirtualPoint + new Size(1, GetFibreHeight());
        }

        public override bool Visible
        {
           set
            {
                base.Visible = value;
               FibresSecond.ForEach(f=>f.Visible = value);
            }
        }
        [XmlIgnore]
        public new SimpleOpticNetDB.CableBoxesRow  Row { get; protected set; }
     
        public void SetRow(SimpleOpticNetDB.CableBoxesRow row)
        {
            Row = row;
            if (Row != null)
            {
                ID = Row.ID;
                Caption = row.IsCaptionNull() ? "" : row.Caption;
            }
            
        }

        protected override void DrawFibre(Graphics graphics)
        {
            base.DrawFibre(graphics);
            FibresSecond.ForEach(item => item.Draw(graphics));
        }

        public override void CreateFibre(int vertexID)
        {
            CreateEquipment(vertexID);
            int i = 0;
            foreach (SimpleOpticNetDB.FibreRow item in Fibres.Cast<SimpleOpticNetDB.FibreRow>())
            {
                // рисуем вниз
                FibreRange fibreRange = new FibreRange(Vis);
                fibreRange.Number = i;
                fibreRange.DobleFibreInVertex = true;
                fibreRange.CurveElementID = ID;
                fibreRange.CurveElementKindID = this.CurveElementKindID;
                fibreRange.Row = item;
                fibreRange.CalcCoordinate(body.VirtualPoint);
                FibresOne.Add(fibreRange);

                // рисуем вверх
                fibreRange = new FibreRange(Vis);
                fibreRange.Number = i;
                fibreRange.DobleFibreInVertex = true;
                fibreRange.CurveElementID = ID;
                fibreRange.CurveElementKindID = this.CurveElementKindID;
                fibreRange.Enter = 2;
                fibreRange.Row = item;
                fibreRange.CalcCoordinate(body.VirtualPoint);
                FibresSecond.Add(fibreRange);
                i++;
            }
        }

        public override void ShiftFibre(Point bodyVirtualPoint)
        {
            List<Task> tasks = new List<Task>();
            for (int i = 0; i < FibresOne.Count; i++)
            {
                FibreRange item = FibresOne[i];
                tasks.Add(Task.Factory.StartNew(delegate { item.CalcCoordinate(bodyVirtualPoint); }));
                // item.CalcCoordinate(bodyVirtualPoint);
            }

            for (int i = 0; i < FibresSecond.Count; i++)
            {
                FibreRange item = FibresSecond[i];
                tasks.Add(Task.Factory.StartNew(delegate { item.CalcCoordinate(bodyVirtualPoint); }));
                // item.CalcCoordinate(bodyVirtualPoint);
            }

            Task.WaitAll(tasks.ToArray());
        }

      
    }
}